package com.tony.ai

import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import com.tony.ai.adapter.CommonAdapter
import com.tony.ai.adapter.RecycleViewDivider
import com.tony.ai.bean.CommonBean
import com.tony.ai.databinding.ActivityMainBinding
import com.tony.ai.ui.ObjectDetectionAndTrackingActivity
import com.tony.ai.ui.SegmentationActivity
import com.tony.ai.ui.TextRecognitionActivity
import com.tony.ai.utils.ActivityUtils
import com.tony.ai.utils.PxUtils
import org.opencv.android.OpenCVLoader

/**
 * AI案例主页面
 * https://blog.csdn.net/qq_30996881/article/details/135177180
 * https://developers.google.cn/ml-kit?hl=ur
 */
class MainActivity : BaseActivity() {
    private val TAG = "MainActivity"
    private lateinit var binding: ActivityMainBinding
    private var datas = ArrayList<CommonBean>()
    private var adapter: CommonAdapter? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        if (!OpenCVLoader.initDebug()) {
            Log.e(TAG, "opencv 初始化失败")
        } else {
            Log.e(TAG, "opencv 初始化成功")
        }
        initViews()
        initPermissions()
    }

    private fun initViews() {
        datas.add(CommonBean(title = "文本识别", TextRecognitionActivity::class.java.name))
        datas.add(CommonBean(title = "图像分割", SegmentationActivity::class.java.name))
        datas.add(CommonBean(title = "对象检测和跟踪", ObjectDetectionAndTrackingActivity::class.java.name))
        initRecyclerView()
    }

    private fun initRecyclerView() {
        val layoutManager =
            LinearLayoutManager(
                this@MainActivity,
                LinearLayoutManager.VERTICAL,
                false
            ) //第二个参数为网格的列数
        binding.recyclerView?.layoutManager = layoutManager
        binding.recyclerView?.addItemDecoration(
            RecycleViewDivider(
                this@MainActivity,
                LinearLayoutManager.VERTICAL,
                PxUtils.dp2px(5f),
                resources.getColor(R.color.white)
            )
        )
        adapter = CommonAdapter { pos, item ->
            ActivityUtils.getInstance()
                .startActivity(this@MainActivity, item.className)
            Toast.makeText(this@MainActivity, "您点击了:${pos},${title}", Toast.LENGTH_LONG).show()
        }
        adapter?.setData(datas)
        binding.recyclerView.adapter = adapter
    }

    private fun initPermissions() {
        var arrays = mutableListOf<String>().apply {
            add("android.permission.WRITE_EXTERNAL_STORAGE")
            add("android.permission.READ_EXTERNAL_STORAGE")
            add("android.permission.CAMERA")
            add("android.permission.RECORD_AUDIO")
            add("android.permission.ACCESS_FINE_LOCATION")
            add("android.permission.ACCESS_COARSE_LOCATION")
//            if (Build.VERSION.SDK_INT >= 29) {
//                add("android.permission.ACCESS_BACKGROUND_LOCATION")
//            }


        }
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
            this.requestPermissions(arrays.toTypedArray(), 100)
        }
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        if (requestCode == 100) {
            for (index in permissions.indices) {
                if (grantResults[index] != PackageManager.PERMISSION_GRANTED) {
                    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
                        requestPermissions(permissions, requestCode)
                    }
                    break
                }
            }
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
    }
}